package weaver.interfaces.jjy.duss.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.jjy.duss.hr.ZFM_HR_032A;
import weaver.interfaces.jjy.duss.hr.ZFM_HR_032B;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *@Description:
 * 年终奖上报（家家悦）流程：当总部审批节点，审批后，
 * ①如果SAP返回成功，则根据年份、月份(推送)、人事范围编码，更新uf_nzjcfhz中的BMSBYJJ、BMSBJTJ，
 * 并将zt更新为2；
 * 根据流程明细中的年份、月份、人员编码，
 * 去更新uf_nzjcfjjy中的YJJ、JTJ；②如果SAP返回失败，则流程停留在当前节点，
 * 不去更新建模中的任何字段；
 *@Author: Dusongsong
 *@Date: 2019/12/24
 */
public class WeaHRnzjJJYtsAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {

        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs4 = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        baseBean.writeLog("请求ID" + requestid);
        baseBean.writeLog("调用: WeaHRnzjJJYtsAction");
        // String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        String workflowid = requestInfo.getWorkflowid();
        rs1.executeQuery("select b.tablename from workflow_base a,workflow_bill b where  b.id=a.formid  and a.id =?", workflowid);
        rs1.next();
        String tablename = Util.null2String(rs1.getString("tablename"));//表名
        //查询主表
        String STATE = "2";//状态
        List<Map<String, String>> list = new ArrayList<>();
        ZFM_HR_032B hr032B = new ZFM_HR_032B();
        rs1.executeQuery("select * from " + tablename + " where requestid=?", requestid);
        baseBean.writeLog("查询主表:" + "select * from " + tablename + " where requestid=" + requestid);
        rs1.next();
        String mainid = Util.null2String(rs1.getString("id"));//外键
        String sqr = Util.null2String(rs1.getString("sqr"));//申请人
        String GJAHR = Util.null2String(rs1.getString("GJAHR"));//年度
        String BMBM = Util.null2String(rs1.getString("BMBM"));//部门编码
        String WERKS = Util.null2String(rs1.getString("WERKS"));//人事范围

        if ("8004".equals(WERKS) || "8007".equals(WERKS) || "ZB01".equals(WERKS)) {
            rs4.executeQuery(" select distinct case when b.field7='ZB01' OR b.field7='8004' or b.field7='8007' then b.field2 when b.field7 not in('ZB01','8004','8007') then c.departmentcode end departmentcode from hrmresource a left join cus_fielddata b on a.id=b.id left join hrmdepartment c on a.departmentid=c.id where a.id='"+sqr+"'");
            rs4.next();
            String sqrid = Util.null2String(rs4.getString("departmentcode"));//部门编码
            baseBean.writeLog("部门编码"+sqrid);
            String sql = "select *  from " + tablename + " where requestid='" + requestid + "' and werks='" + WERKS + "' and bmbm='" + BMBM + "' and gjahr='" + GJAHR + "'";
            rs1.executeQuery(sql);
            baseBean.writeLog(sql);
            rs1.next();
            String BMGDYJJ = Util.null2String(rs1.getString("BMGDYJJ"));//部门规定业绩奖
            String BMGDJTJ = Util.null2String(rs1.getString("BMGDJTJ"));//部门规定集体奖
            String BMSBYJJ = Util.null2String(rs1.getString("BMSBYJJ"));//部门上报业绩奖
            String BMSBJTJ = Util.null2String(rs1.getString("BMSBJTJ"));//部门上报集体奖
            Map<String, String> map1 = new HashMap();
            map1.put("GJAHR", GJAHR);
            map1.put("BMBM", sqrid);
            map1.put("WERKS", WERKS);
            map1.put("BMGDYJJ", BMGDYJJ);
            map1.put("BMGDJTJ", BMGDJTJ);
            map1.put("BMSBYJJ", BMSBYJJ);
            map1.put("BMSBJTJ", BMSBJTJ);
            baseBean.writeLog("相关参数:" + map1.toString());
            //明细表
            String SQL1 = "select * from " + tablename + "_dt1 where mainid='" + mainid + "' and werks='" + WERKS + "' and bmbm='" + BMBM + "' and gjahr='" + GJAHR + "'";
            rs2.execute(SQL1);//查询明细表
            baseBean.writeLog("查询明细：" + SQL1);
            try {
                while (rs2.next()) {
                    String PERNR = Util.null2String(rs2.getString("PERNR"));         //人员编码
                    String JTJ = Util.null2String(rs2.getString("JTJ"));              //集体奖
                    String YJJ = Util.null2String(rs2.getString("YJJ"));             //业绩奖
                    Map<String, String> map2 = new HashMap();
                    map2.put("GJAHR", GJAHR);
                    map2.put("BMBM", BMBM);
                    map2.put("PERNR", PERNR);
                    map2.put("JTJ", JTJ);
                    map2.put("YJJ", YJJ);
                    list.add(map2);
                    baseBean.writeLog("相关参数:" + map2.toString());
                }

                Map resultMap2 = hr032B.execute(list);
                String flag2 = (String) resultMap2.get("flag");
                String message2 = (String) resultMap2.get("message");
                baseBean.writeLog("SAP返回结果——flag:" + flag2 + ";message:" + message2);
                if ("s".equalsIgnoreCase(flag2)) {
                    rs2.execute(SQL1);//查询明细表
                    while (rs2.next()) {
                        String PERNR = Util.null2String(rs2.getString("PERNR"));         //人员编码
                        String JTJ = Util.null2String(rs2.getString("JTJ"));              //集体奖
                        String YJJ = Util.null2String(rs2.getString("YJJ"));             //业绩奖
                        String updateSQL2 = "update uf_nzjcfjjy set jtj='" + JTJ + "' ,yjj= '" + YJJ + "' where werks='" + WERKS + "' and gjahr='" + GJAHR + "' and pernr='" + PERNR + "'";
                        rs1.execute(updateSQL2);
                        baseBean.writeLog("调用SQL:" + updateSQL2);
                    }

                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message2);
                    return Action.FAILURE_AND_CONTINUE;
                }
                ZFM_HR_032A hr_032A = new ZFM_HR_032A();
                Map resultMap1 = hr_032A.execute(map1);
                String flag1 = (String) resultMap1.get("flag");
                String message1 = (String) resultMap1.get("message");
                baseBean.writeLog("SAP返回结果——flag:" + flag1 + ";message:" + message1);
                if ("s".equalsIgnoreCase(flag1)) {
                    String updateSQL1 = "update uf_nzjcfhz set bmsbyjj='" + BMSBYJJ + "', bmsbjtj='" + BMSBJTJ + "',zt='" + STATE + "' where gjahr='" + GJAHR + "' and bmbm='" + BMBM + "' and werks='" + WERKS + "' ";
                    rs1.executeUpdate(updateSQL1);
                    baseBean.writeLog("执行语句：" + updateSQL1);
                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message1);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } catch (Exception e) {
                e.printStackTrace();
                baseBean.writeLog("异常参考:" + e);
                requestInfo.getRequestManager().setMessageid("111100");
                requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } else {
            String sql = "select *  from " + tablename + " where requestid='" + requestid + "' and werks='" + WERKS + "' and gjahr='" + GJAHR + "'";
            rs1.executeQuery(sql);
            baseBean.writeLog(sql);
            rs1.next();
            String BMGDYJJ = Util.null2String(rs1.getString("BMGDYJJ"));//部门规定业绩奖
            String BMGDJTJ = Util.null2String(rs1.getString("BMGDJTJ"));//部门规定集体奖
            String BMSBYJJ = Util.null2String(rs1.getString("BMSBYJJ"));//部门上报业绩奖
            String BMSBJTJ = Util.null2String(rs1.getString("BMSBJTJ"));//部门上报集体奖
            Map<String, String> map1 = new HashMap();
            map1.put("GJAHR", GJAHR);
            map1.put("BMBM", "00000000");
            map1.put("WERKS", WERKS);
            map1.put("BMGDYJJ", BMGDYJJ);
            map1.put("BMGDJTJ", BMGDJTJ);
            map1.put("BMSBYJJ", BMSBYJJ);
            map1.put("BMSBJTJ", BMSBJTJ);
            baseBean.writeLog("相关参数:" + map1.toString());
            //明细表
            String SQL1 = "select * from " + tablename + "_dt1 where mainid='" + mainid + "' and werks='" + WERKS + "' and gjahr='" + GJAHR + "'";
            rs2.execute(SQL1);//查询明细表
            baseBean.writeLog("查询明细：" + SQL1);
            try {
                while (rs2.next()) {
                    String PERNR = Util.null2String(rs2.getString("PERNR"));         //人员编码
                    String JTJ = Util.null2String(rs2.getString("JTJ"));              //集体奖
                    String YJJ = Util.null2String(rs2.getString("YJJ"));             //业绩奖
                    Map<String, String> map2 = new HashMap();
                    map2.put("GJAHR", GJAHR);
                    map2.put("BMBM", BMBM);
                    map2.put("PERNR", PERNR);
                    map2.put("JTJ", JTJ);
                    map2.put("YJJ", YJJ);
                    list.add(map2);
                    baseBean.writeLog("相关参数:" + map2.toString());
                }

                Map resultMap2 = hr032B.execute(list);
                String flag2 = (String) resultMap2.get("flag");
                String message2 = (String) resultMap2.get("message");
                baseBean.writeLog("SAP返回结果——flag:" + flag2 + ";message:" + message2);
                if ("s".equalsIgnoreCase(flag2)) {
                    rs2.execute(SQL1);//查询明细表
                    while (rs2.next()) {
                        String PERNR = Util.null2String(rs2.getString("PERNR"));         //人员编码
                        String JTJ = Util.null2String(rs2.getString("JTJ"));              //集体奖
                        String YJJ = Util.null2String(rs2.getString("YJJ"));             //业绩奖
                        String updateSQL2 = "update uf_nzjcfjjy set jtj='" + JTJ + "' ,yjj= '" + YJJ + "' where werks='" + WERKS + "' and gjahr='" + GJAHR + "' and pernr='" + PERNR + "'";
                        rs1.execute(updateSQL2);
                        baseBean.writeLog("调用SQL:" + updateSQL2);
                    }

                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message2);
                    return Action.FAILURE_AND_CONTINUE;
                }
                ZFM_HR_032A hr_032A = new ZFM_HR_032A();
                Map resultMap1 = hr_032A.execute(map1);
                String flag1 = (String) resultMap1.get("flag");
                String message1 = (String) resultMap1.get("message");
                baseBean.writeLog("SAP返回结果——flag:" + flag1 + ";message:" + message1);
                if ("s".equalsIgnoreCase(flag1)) {
                    String updateSQL1 = "update uf_nzjcfhz set bmsbyjj='" + BMSBYJJ + "', bmsbjtj='" + BMSBJTJ + "',zt='" + STATE + "' where gjahr='" + GJAHR + "' and werks='" + WERKS + "' ";
                    rs1.executeUpdate(updateSQL1);
                    baseBean.writeLog("执行语句：" + updateSQL1);
                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message1);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } catch (Exception e) {
                e.printStackTrace();
                baseBean.writeLog("异常参考:" + e);
                requestInfo.getRequestManager().setMessageid("111100");
                requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        }
    }
}

